<?php 
include("common/conn.php");
if(!isset($_SESSION['userid'])){
    echo "<script>window.location.href='login.php';</script>";
}

// 获取所有数据
$nodes = array();

fetch_all($thesymptom);
$arr = $nodes;
// -----------------------------

class Node{
    public $id;
    public $pId;
    public $name;
    public $kuaidiyuan;
    public $phone;
    public $address;
    
    public function __construct($id, $pId, $name, $kuaidiyuan, $phone,$address){
        $this->id = $id;
        $this->pId = $pId;
        $this->name = $name;
        $this->kuaidiyuan = $kuaidiyuan;
        $this->phone = $phone;
        $this->phone = $address;
    }
}

function fetch_all($thesymptom){
    global $nodes;
    $result = mysql_query("select * from quyu_kuai");
     while($row = mysql_fetch_assoc($result)){
        $node_item = new Node($row[id], $row[pid] , $row[district], $row[kuaidiyuan], $row[phone], $row[address]);
        $nodes[] = $node_item;
     }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <!-- 权限控制 -->
        <link type="text/css"  rel="stylesheet"  href="css/zTreeStyle/zTreeStyle.css"/>
        <link rel="stylesheet" href="css/css.css" type="text/css">
        <script src="js/jquery-2.1.0.js"></script>
        <script type="text/javascript" src="js/jquery.ztree.all-3.5.min.js"></script>
        <!-- 对话框插件 -->
        <script src="js/artDialog/jquery.artDialog.js?skin=twitter"></script>
        <script src="js/artDialog/plugins/iframeTools.js"></script>
         <!-- 表单库插件 -->
        <script type="text/javascript" src="js/jquery.form.min.js"></script>
        <!-- 配置artDialog全局默认参数 -->
        <script type="text/javascript">
        (function (config) {
            config['lock'] = true;
            config['fixed'] = true;
            config['top'] = '50%';
        })(art.dialog.defaults);
        </script>
        <style type="text/css">
              .ztree li span.button.add {margin-left:2px; margin-right: -1px; background-position:-144px 0; vertical-align:top; *vertical-align:middle}
            /* 右键菜单 */
               div#rMenu {position:absolute; visibility:hidden; top:0; background-color: #555;text-align: left;padding: 2px;}
               div#rMenu ul li{
                margin: 1px 0;
                padding: 0 5px;
                cursor: pointer;
                list-style: none outside none;
                background-color: #DFDFDF;
            }
                ul.ztree {
                background: none repeat scroll 0 0 #F0F6E4;
                border: 1px solid #617775;
                height: 600px;
                margin-top: 10px;
                overflow-x: auto;
                overflow-y: scroll;
                width: 100%;
                padding:5px;
            }
            </style>
    <SCRIPT type="text/javascript">
                var setting = {
                    view: {
                        addHoverDom: addHoverDom,
                        removeHoverDom: removeHoverDom,
                    },
                    edit: {
                        enable: true,
                        showRemoveBtn: true,
                        showRenameBtn: true
                    },
                    data: {
                        simpleData: {
                            enable: true
                        }
                    },
                    callback: {
                        beforeRemove: beforeRemove,
                        beforeEditName: beforeEditName,
                        onRightClick: OnRightClick
                    }
                };

                //var zNodes = $.parseJSON('<?php echo json_encode($arr); ?>');

                var zNodes = <?php echo json_encode($arr); ?>;

                //捕获节点被删除之前的事件回调函数
                function beforeRemove(treeId, treeNode) {
                    /**if(treeNode.is_left==0){
                        art.dialog('根节点不允许删除', function(){
                            window.location.href = "guidance.php?time="+new Date().getTime();
                            
                        });
                        return;
                    }**/
                    var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                    zTree.selectNode(treeNode);
                    $.dialog.confirm("确认删除 节点 -- " + treeNode.name + " 吗？",function(){
                        $.post("delquyuKuaidi.php?",{id:treeNode.id},
                          function(data){
                            window.location.href = "quyuKuaidi.php?time="+new Date().getTime();
                        });
                    });
                }

                //捕获节点编辑按钮的 click 事件
                function beforeEditName(treeId, treeNode) {
                    var treeId = treeNode.id;
                    $.dialog.open("updatequyuKuaidi.php?id="+treeId,{
                        title: '更新菜单项', 
                        width: 500,
                        height: 250,
                        close:function(){
                            window.location.href = "quyuKuaidi.php?time="+new Date().getTime();
                    }});
                    return false;
                }

                //添加一个增加节点的按钮
                function addHoverDom(treeId, treeNode) {
                    var sObj = $("#" + treeNode.tId + "_span");
                    if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
                    var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
                        + "' title='add node' onfocus='this.blur();'></span>";
                    sObj.after(addStr);
                    var btn = $("#addBtn_"+treeNode.tId);
                    if (btn) btn.bind("click", function(){
                       
                       var pId = treeNode.id;
                       /**$.dialog({
                            title: '增加子节点',
                            content: $("#addPurviewDiv").html(),
                            ok: function () {
                                $("input[name='pid']").val(pId);
                                $("#purview").ajaxSubmit({
                                    url:'addguidance.php',
                                    type:"POST",
                                    success:function(data){
                                        $.dialog.close();
                                        
                                    }           
                                }); 
                            },
                            okVal:'提交',
                            cancel: true,
                            cancelVal:'取消',
                            close:function(){
                               window.location.href = "guidance.php?symptom=<?php echo $thesymptom;?>&bodys=<?php echo $thebodys;?>&group=<?php echo $theGroup;?>&time="+new Date().getTime();
                            }
                        });**/
                        $.dialog.open("updatequyuKuaidi.php?pid="+pId,{
                                title: '增加子节点', 
                                width: 500,
                                height: 250,
                                close:function(){
                                     window.location.href = "quyuKuaidi.php?time="+new Date().getTime();
                            }});
                            return false;
                        
                    });
                };

                //为插件定义：卸载增加按钮过程
                function removeHoverDom(treeId, treeNode){
                    $("#addBtn_"+treeNode.tId).unbind().remove();
                };

                //增加右键菜单：增加根节点
                function OnRightClick(event, treeId, treeNode) {
                            if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
                                showRMenu("root", event.clientX, event.clientY);
                            }
                        }

                function showRMenu(type, x, y) {
                    $("#rMenu ul").show();
                    rMenu.css({"top":(y-130+$(document).scrollTop())+"px", "left":(x-320)+"px", "visibility":"visible"});
                    $("body").bind("mousedown", onBodyMouseDown);
                }

                function onBodyMouseDown(event){
                    if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {
                        rMenu.css({"visibility" : "hidden"});
                    }
                }

                function hideRMenu() {
                    if (rMenu) rMenu.css({"visibility": "hidden"});
                    $("body").unbind("mousedown", onBodyMouseDown);
                }

                //异步增加一个根节点
                function addTreeRootNode() {
                    hideRMenu();
                    /**$.dialog({
                        title: '增加根节点',
                        content: $("#addPurviewDiv").html(),
                        ok: function () {
                            $("input[name='pid']").val(0);
                            $("#purview").ajaxSubmit({
                                url:'addguidance.php',
                                type:"POST",
                                success:function(data){
                                    $.dialog.close();
                                    
                                }           
                            }); 
                        },
                        okVal:'提交',
                        cancel: true,
                        cancelVal:'取消',
                        close:function(){
                            window.location.href = "guidance.php?symptom=<?php echo $thesymptom;?>&bodys=<?php echo $thebodys;?>&group=<?php echo $theGroup;?>&time="+new Date().getTime();
                        }
                    });**/
                    $.dialog.open("updatequyuKuaidi.php?pid=0",{
                                title: '增加根节点', 
                                width: 500,
                                height: 250,
                                close:function(){
                                    window.location.href = "quyuKuaidi.php?time="+new Date().getTime();
                            }});
                            return false;
                }


                function change(obj){
                     if(obj.value==0){
                        $('#disease_div').hide();
                     }else{
                        $('#disease_div').show();
                     }
                    
                }


                //页面加载完后加载整棵树
                var rMenu;
                $(document).ready(function(){
                    $.fn.zTree.init($("#treeDemo"), setting, zNodes);
                    var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
                    treeObj.expandAll(true);
                    rMenu = $("#rMenu");
                });
    </SCRIPT>
  <link href="css/styles.css" rel="stylesheet" type="text/css" />
  <link href="css/style2.css" rel="stylesheet" type="text/css" />
  <link href="css/css.css" rel="stylesheet" type="text/css" />
  <style>
    .move_up,.move_down{
      margin-top: 0.5em;
      padding-top: 0.5em;
      display: inline-block;
      width: 4em !important;
      height: 2em !important;
      background-color: #FF963A !important;
      border-radius: 10px;
      margin: 0 0.5em 0.5em 0.5em;
    }
  </style>
</head>
<body>
 <!--header-->
  <?php include("common/header.php");?>
  <!--header end-->
      <div id="main">
      <center>
        <span id="left_show"><h5  style="color:red;">说明：(1.最多四级,省/市/区/街道  &nbsp;&nbsp;&nbsp;&nbsp;2.在下方展示区域内，点击鼠标右键增加省份)</h5></span>
        
         <!--内容开始-->
        <div class="mian">    
        <div style="margin-top:20px;width:50%;height:100px;">
            
            <div class="zTreeDemoBackground left">
                <ul id="treeDemo" class="ztree"></ul>
            </div>
            <div id="rMenu">
                <ul>
                     <li id="m_add" onclick="addTreeRootNode();">增加省份（根节点）</li>
                </ul>
            </div>

              </div>
               </div>
        </center>
         </div>
     <!-- 增加节点结束 -->
    </body>
</html>